import deepCopy from '../../pub/deepcopy'
import { isType } from '../utils'

class Model {
  _model = {}

  onCreated(){
    if ( isType(this.model, 'Function') ){
      this._model = createModel( this.model() )
    } else if (isType(this.model, 'Objdec')) {
      this._model = createModel(this.model)
    }
  }
  // get model(){
  //   return this._model
  // }
  
  // set model(){
  //   console.warn('Cannt set the Model!')
  // }

  // init( model = {} ){
  //   this._model = createModel(model)
  // }
}

function createModel( model ){
  const obj = deepCopy( model )
  const o = new Proxy(
    obj,
    {
      get (target, key, receiver) {
        return target[key]
      },
      set (target, prop, value) {
        const re = Reflect.set(...arguments)
        return re
      }
    }
  )

  const fn = ( d )=>{
    for(let key of Reflect.ownKeys(d)){
      fn(d[key])
    }
  }
  return new Proxy(
    obj,
    {
      get ( target, key, receiver ) {
        return target[key]
      },
      set ( target, prop, value ) {
        const re = Reflect.set(...arguments)
        
        return re
      }
    }
  )
}

export default Model